package com.xiaouyudeguang.common.ditable.resource;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xiaouyudeguang.common.constants.Headers;
import com.xiaouyudeguang.common.ditable.service.DiTableService;
import com.xiaouyudeguang.common.respose.ResultDto;
import com.xiaouyudeguang.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping(value = "/ditable")
public class DiTableResource {

    @Autowired
    private DiTableService baseService;

    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public ResultDto<String> save(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                  @RequestHeader(Headers.APP_ID) String appId,
                                  @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.save(tenantId, appId, request));
    }

    @RequestMapping(method = RequestMethod.POST, value = "/saveBatch")
    public ResultDto<Boolean> saveBatch(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                        @RequestHeader(Headers.APP_ID) String appId,
                                        @RequestBody List<JsonNode> dataList) throws Exception {
        return ResultDto.ok(baseService.saveBatch(tenantId, appId, dataList));
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/deleteById/{id}")
    public ResultDto<Boolean> deleteById(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                         @RequestHeader(Headers.APP_ID) String appId,
                                         @PathVariable("id") String id) throws Exception {
        return ResultDto.ok(baseService.deleteById(tenantId, appId, id));
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/deleteByIds")
    public ResultDto<Boolean> deleteByIds(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                          @RequestHeader(Headers.APP_ID) String appId,
                                          @RequestBody Set<String> ids) throws Exception {
        return ResultDto.ok(baseService.deleteByIds(tenantId, appId, ids));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/updateById")
    public ResultDto<Boolean> updateById(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                         @RequestHeader(Headers.APP_ID) String appId,
                                         @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.updateById(tenantId, appId, request));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/updateBatch")
    public ResultDto<Boolean> updateBatch(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                          @RequestHeader(Headers.APP_ID) String appId,
                                          @RequestBody List<JsonNode> dataList) throws Exception {
        return ResultDto.ok(baseService.updateBatch(tenantId, appId, dataList));
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getById/{id}")
    public ResultDto<JsonNode> getById(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                       @RequestHeader(Headers.APP_ID) String appId,
                                       @PathVariable("id") String id) throws Exception {
        return ResultDto.ok(baseService.getById(tenantId, appId, id));
    }

    @RequestMapping(method = RequestMethod.POST, value = "/getByIds")
    public ResultDto<List<ObjectNode>> getByIds(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                                @RequestHeader(Headers.APP_ID) String appId,
                                                @RequestBody Set<String> ids) throws Exception {
        return ResultDto.ok(baseService.getByIds(tenantId, appId, ids));
    }

    @RequestMapping(method = RequestMethod.POST, value = "/list")
    public ResultDto<List<ObjectNode>> list(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                            @RequestHeader(Headers.APP_ID) String appId,
                                            @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.list(tenantId, appId, request));
    }

    @RequestMapping(method = RequestMethod.POST, value = "/page")
    public ResultDto<PageImpl<ObjectNode>> page(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                                @RequestHeader(Headers.APP_ID) String appId,
                                                @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.page(tenantId, appId, request));
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{table_name}/getTable")
    public ResultDto<ObjectNode> getTable(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                          @RequestHeader(Headers.APP_ID) String appId,
                                          @PathVariable("table_name") String tableName) throws Exception {
        return ResultDto.ok(baseService.getTable(tenantId, appId, StringUtils.lowerCamel(tableName)));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{table_name}/addColumn")
    public ResultDto<Boolean> addColumn(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                        @RequestHeader(Headers.APP_ID) String appId,
                                        @PathVariable("table_name") String tableName,
                                        @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.addColumn(tenantId, appId, StringUtils.lowerCamel(tableName), request));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{table_name}/updateColumn")
    public ResultDto<Boolean> updateColumn(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                           @RequestHeader(Headers.APP_ID) String appId,
                                           @PathVariable("table_name") String tableName,
                                           @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.updateColumn(tenantId, appId, StringUtils.lowerCamel(tableName), request));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{table_name}/deleteColumn")
    public ResultDto<Boolean> deleteColumn(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                           @RequestHeader(Headers.APP_ID) String appId,
                                           @PathVariable("table_name") String tableName,
                                           @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.deleteColumn(tenantId, appId, StringUtils.lowerCamel(tableName), request));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{table_name}/addAction")
    public ResultDto<Boolean> addAction(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                        @RequestHeader(Headers.APP_ID) String appId,
                                        @PathVariable("table_name") String tableName,
                                        @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.addAction(tenantId, appId, StringUtils.lowerCamel(tableName), request));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{table_name}/updateAction")
    public ResultDto<Boolean> updateAction(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                           @RequestHeader(Headers.APP_ID) String appId,
                                           @PathVariable("table_name") String tableName,
                                           @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.updateAction(tenantId, appId, StringUtils.lowerCamel(tableName), request));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{table_name}/deleteAction")
    public ResultDto<Boolean> deleteAction(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                           @RequestHeader(Headers.APP_ID) String appId,
                                           @PathVariable("table_name") String tableName,
                                           @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.deleteAction(tenantId, appId, StringUtils.lowerCamel(tableName), request));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{table_name}/addTab")
    public ResultDto<Boolean> addTab(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                     @RequestHeader(Headers.APP_ID) String appId,
                                     @PathVariable("table_name") String tableName,
                                     @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.addTab(tenantId, appId, StringUtils.lowerCamel(tableName), request));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{table_name}/updateTab")
    public ResultDto<Boolean> updateTab(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                        @RequestHeader(Headers.APP_ID) String appId,
                                        @PathVariable("table_name") String tableName,
                                        @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.updateTab(tenantId, appId, StringUtils.lowerCamel(tableName), request));
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{table_name}/deleteTab")
    public ResultDto<Boolean> deleteTab(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                        @RequestHeader(Headers.APP_ID) String appId,
                                        @PathVariable("table_name") String tableName,
                                        @RequestBody JsonNode request) throws Exception {
        return ResultDto.ok(baseService.deleteTab(tenantId, appId, StringUtils.lowerCamel(tableName), request));
    }
}
